package cn.com.eado.crm.v.weiget

import android.content.Context
import android.content.res.TypedArray
import android.text.Editable
import android.util.AttributeSet
import android.view.LayoutInflater
import android.widget.LinearLayout
import cn.com.eado.crm.R
import cn.com.eado.crm.app.utils.BasisTimesUtils
import kotlinx.android.synthetic.main.auto_spinner.view.tvName
import kotlinx.android.synthetic.main.date_spinner.view.*
import java.util.*


class DateSpinner (ctx: Context, attributeSet: AttributeSet?, defStyleAttr: Int) : LinearLayout(ctx, attributeSet, defStyleAttr){
    constructor(ctx: Context, attributeSet: AttributeSet?) : this(ctx, attributeSet, 0)
    constructor(ctx: Context) : this(ctx, null)

    init {
        LayoutInflater.from(ctx).inflate(R.layout.date_spinner, this)
        //
        val typedArray: TypedArray = ctx.obtainStyledAttributes(attributeSet, R.styleable.AutoSpinner)
        val name = typedArray.getString(R.styleable.AutoSpinner_name)
        //
        tvName.text = name
        ivDatePick.setOnClickListener {
            showDate(ctx)
        }
    }

    /**
     * 年月日选择
     */
    private fun showDate(context: Context) {
        val calendar = Calendar.getInstance()
        BasisTimesUtils.showDatePickerDialog(
            context,
            BasisTimesUtils.THEME_HOLO_DARK,
            "请选择年月日",
            calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH)+1, calendar.get(Calendar.DAY_OF_MONTH),
            object :
                BasisTimesUtils.OnDatePickerListener {

                override fun onConfirm(year: Int, month: Int, dayOfMonth: Int) {
//                    context as Activity
//                    context.toast("$year-$month-$dayOfMonth")
                    etDate.text = Editable.Factory.getInstance().newEditable("$year-$month-$dayOfMonth")
                }

                override fun onCancel() {
//                    context as Activity
//                    context.toast("cancel")
                }
            })
    }

    fun getText() : String {
        return etDate.text.toString()
    }

    fun getName() : String {
        val s = tvName.text.toString().trim()
        return s.replace(":","").replace("：", "")
    }
}